package com.example.rollcallsapplication;

import static java.lang.System.exit;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;


public class MainActivity extends AppCompatActivity {

    String area="男生";

    int num;

    private RadioGroup mRg1;

    private EditText mEtnum;

    private TextView mTvtime;

    private LinearLayout mLlnames;

    public static List<Activity> activityList = new LinkedList();

    private Button mBt1,mBt2;

    private final String mFilename="namelist.txt";

    List<String> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRg1=(RadioGroup) findViewById(R.id.rg_1);
        mRg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton1=(RadioButton) radioGroup.findViewById(i);
                area= (String) radioButton1.getText();

            }
        });



//        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);

//        read();
//        Toast.makeText(MainActivity.this, list.get(5),Toast.LENGTH_SHORT).show();


//        mLlnames=(LinearLayout)findViewById(R.id.ll_1);
//        TextView textView=new TextView(this);
//        textView.setText("我是自创的");
//        textView.setTextColor(Color.parseColor("#FF6400"));
//        textView.setTextSize(24);
//        mLlnames.addView(textView);




//        mTvtime.setText("当前时间：");

        mBt1=(Button) findViewById(R.id.btn_1);
        mBt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mEtnum=(EditText) findViewById(R.id.et_1);
                mLlnames=(LinearLayout)findViewById(R.id.ll_1);
                if(mLlnames.getChildCount()>0){
                    int len=mLlnames.getChildCount();
                    for(int i=0;i<len;i++){
                        mLlnames.removeViewAt(mLlnames.getChildCount()-1);
                    }
                }

                try{

                    int maxx=21;
                    switch(area){
                        case "男生":maxx=15;break;
                        case "女生":maxx=9;break;
                        case "全体":maxx=24;break;
                    }

                    num=Integer.parseInt(mEtnum.getText().toString());
                    if(num<=0 || num >maxx){
                        throw new NumberFormatException();
                    }

//                    list=null;
                    list.clear();

                    //读入文件

                    read();
//                    mLlnames=(LinearLayout)findViewById(R.id.ll_1);
//                    TextView textView=new TextView(MainActivity.this);
//                    textView.setText("我运行到了这");
//                    textView.setTextColor(Color.parseColor("#FF6400"));
//                    textView.setTextSize(24);
//                    mLlnames.addView(textView);
//
//
//                    //输出被点名者
                    for(int i=0;i<num;i++){
                        int index=(int)(Math.random()*list.size());
                        String ans= list.get(index);
                        list.remove(ans);
                        TextView textView=new TextView(MainActivity.this);
                        textView.setText(ans);
                        textView.setTextColor(Color.parseColor("#FF6400"));
                        textView.setTextSize(24);
                        mLlnames.addView(textView);
                    }

                    mTvtime=(TextView) findViewById(R.id.tv_5);
                    SimpleDateFormat formatter=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                    Date curDate=new Date(System.currentTimeMillis());
                    mTvtime.setText("当前时间："+formatter.format(curDate));


                }catch (Exception e){
                    Toast.makeText(MainActivity.this, "请输入正确的数字",Toast.LENGTH_SHORT).show();
                }

            }
        });

        mBt2=(Button) findViewById(R.id.btn_2);
        mBt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                exit();
            }
        });
        MainActivity.activityList.add(this);

    }



    //退出
    public void exit(){

        for(Activity act:activityList){

            act.finish();

        }

        System.exit(0);

    }

    //读取信息
    public String read(){
        int i=0;
        String ln=null;
        InputStream inputStream=null;
        switch (area){
            case "男生":inputStream=getResources().openRawResource(R.raw.boy);;break;
            case "女生":inputStream=getResources().openRawResource(R.raw.girl);;break;
            case "全体":inputStream=getResources().openRawResource(R.raw.namelist);;break;
        }
        InputStreamReader inputStreamReader=null;
        try {
            inputStreamReader=new InputStreamReader(inputStream,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
        while(i<50){
                try {
                    if(!((ln=bufferedReader.readLine())!=null)){
                        break;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                list.add(ln);
                i++;
        }
        return null;
    }

}

//后续：数量不满足处理

//public class rollcall1_0 {
//
//    public static void main(String[] args) throws IOException{
//
//        List<String> list=new ArrayList<>();
//
//        while(true){
//            Scanner in0=new Scanner(System.in);
//            System.out.println("男生、女生还是全体？");
//            String type=in0.nextLine();
//            Scanner in1;
//            if(type.equals("全体")){
//                File infile = new File("D:\\develop\\Java\\programs\\rollcall\\src\\namelist.txt");
//                in1=new Scanner(infile,"UTF-8");
//            }
//            else if(type.equals("男生")){
//                File infile = new File("D:\\develop\\Java\\programs\\rollcall\\src\\boy.txt");
//                in1=new Scanner(infile,"UTF-8");
//            }
//            else{
//                File infile = new File("D:\\develop\\Java\\programs\\rollcall\\src\\girl.txt");
//                in1=new Scanner(infile,"UTF-8");
//            }
//
//
//            while(in1.hasNext()){
//                String s=in1.nextLine();
//                list.add(s);
//            }
//
//            Scanner innum=new Scanner(System.in);
//            System.out.println("请输入人数：");
//            int num=innum.nextInt();
//            for(int i=0;i<num;i++){
//                int index=(int)(Math.random()*list.size());
//                String ans= list.get(index);
//                list.remove(ans);
//                System.out.println(ans);
//            }
//
//            LocalDateTime dateTime = LocalDateTime.now();
//            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss");
//            System.out.println("当前时间为"+dateTime.format(formatter));
//
//
//            in1.close();
//            Scanner inlast=new Scanner(System.in);
//            System.out.println("继续还是结束？");
//            String ifend=inlast.nextLine();
//            if(ifend.equals("结束"))break;
//        }
//
//
//
//    }
//}